import 'dart:async';
import 'package:dio/dio.dart';
import 'package:test01/okhttp_test/pojo/attention_pojo.dart';
import 'package:test01/okhttp_test/pojo/commentdto_pojo.dart';
import 'package:test01/okhttp_test/pojo/login_request_pojo.dart';
import 'package:test01/okhttp_test/pojo/saveBasicFitnessInfo_pojo.dart';
import 'package:test01/okhttp_test/pojo/serach_pojo.dart';
import 'package:test01/okhttp_test/pojo/userinfor_pojo.dart';

String tokens="bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEwMDQwNzEsInVzZXJfbmFtZSI6IjEwMDQwNzEiLCJzY29wZSI6WyJhcHB1c2VyIiwidWNlbnRlciIsInVhYSIsInRoaXJkIiwidGVybWluYWwiLCJzcG9ydCIsImVzIiwiYWdvcmEiXSwiaXNQaG9uZSI6MSwiaXNQZXJmZWN0IjoxLCJtaWQiOm51bGwsImFub255bW91cyI6MCwiZXhwIjoxNjMwNDkxMDkzLCJncmFudFR5cGUiOjAsImp0aSI6IjJiMWY2ZjA3LTBlMmQtNDMzOC04ZTg0LTNkYzIwNDRhZTlkMCIsImNsaWVudF9pZCI6InVzZXJBcHAifQ.qO5Ey346rfLNIeNOoEfRBfko8o7RxP_u_suNWOD10Q0";



String refreshtokens;

String url = "http://ifdev.aimymusic.com";

///界面初始化-获取token
///个人主页
Future getinittokens(LoginRequst loginRequst, String interface) async {
  LoginRespons loginRespons = LoginRespons();
  Dio dio = Dio();
  dio.options.headers["Authorization"] = loginRequst.authorization;
  print('开始网络请求');
  Response response = await dio.post(url + interface, queryParameters: {
    'grant_type': loginRequst.grant_type,
    'username': loginRequst.username,
    'code': loginRequst.code,
    'refresh_token': loginRequst.refresh_token,
  });
  print(response);
  if (response.statusCode == 200) {
    loginRespons = LoginRespons.fromJson(response.data["data"]);
  }
  return loginRespons;
}

///个人主页
Future getloginsend(LoginSend loginSend, String interface) async {
  print("调用异步------------------------------------------------------------");
  Dio dio = Dio();
  dio.options.headers["Authorization"] = loginSend.authorizations;
  print('开始网络请求');
  Response response = await dio.post(url + interface, queryParameters: {
    'phoneNumber': loginSend.phoneNumber,
    'type': loginSend.type,
    'anonymousToken': loginSend.anonymousToken,
  });
  print(response);
  if (response.statusCode == 200) {
    return true;
  } else {
    return false;
  }
}

///个人主页
Future getlistmodelmore(int lastTime, int size, String interface) async {
  CommentDto commentDtoList = CommentDto();
  print("调用异步------------------------------------------------------------");

  Dio dio = Dio();
  dio.options.headers["aimy-drivers"] =
      '{\"os\":0,\"clientVersion\":\"1.0.0\",\"channel\":0}';
  dio.options.headers["Authorization"] = tokens;
  print('开始网络请求');
  Response response = await dio.post(url + interface, queryParameters: {
    'type': 0,
    'size': size,
    'lastTime': lastTime,
  });
  print(response);
  if (response.statusCode == 200) {
    commentDtoList = CommentDto.fromJson(response.data["data"]);
  }
  return commentDtoList;
}

///关注页面
Future aa(int size, String interface) async {
  CommentDto commentDto;
  Dio dio = Dio();
  dio.options.headers["aimy-drivers"] =
      '{\"os\":0,\"clientVersion\":\"1.0.0\",\"channel\":0}';
  dio.options.headers["Authorization"] = tokens;
  print('开始网络请求');
  Response response = await dio.post(url + interface, queryParameters: {
    'type': 0,
    'size': size,
  });
  print(response);
  if (response.statusCode == 200) {
    commentDto = CommentDto.fromJson(response.data["data"]);
  }
  return commentDto;
}

///热门搜索词汇
Future hotWordsApi(String interface) async {
  HotWords hotwords;

  List<HotWordsList> hotwordslist = [];

  Dio dio = Dio();
  dio.options.headers["aimy-drivers"] =
      '{\"os\":0,\"clientVersion\":\"1.0.0\",\"channel\":0}';
  dio.options.headers["Authorization"] = tokens;
  print('开始网络请求');
  Response response = await dio.post(url + interface);
  print(response);
  if (response.statusCode == 200) {
    print("调用对象类");
    hotwords = HotWords.fromJson(response.data["data"]);

    ///遍历获取
    hotwords.list.forEach((element) {
      hotwordslist.add(element);
    });
  }
  return hotwordslist;
}

///热门话题
Future hotTopicApi(int size, String interface) async {
  HotTopic hottopic;
  List<HotToppicList> hottoppiclist = [];

  Dio dio = Dio();
  dio.options.headers["aimy-drivers"] =
      '{\"os\":0,\"clientVersion\":\"1.0.0\",\"channel\":0}';
  dio.options.headers["Authorization"] = tokens;
  print('开始网络请求');
  Response response = await dio.post(url + interface, queryParameters: {
    'size': size,
  });
  print(response);
  if (response.statusCode == 200) {
    print("调用对象类");
    hottopic = HotTopic.fromJson(response.data["data"]);

    ///遍历获取
    hottopic.list.forEach((element) {
      hottoppiclist.add(element);
    });
  }
  return hottoppiclist;
}



///推荐课题
Future recommendedTopics(int type,int size,String interface) async {
  CommentDto commentDto;
  Dio dio = Dio();
  dio.options.headers["aimy-drivers"] =
  '{\"os\":0,\"clientVersion\":\"1.0.0\",\"channel\":0}';
  dio.options.headers["Authorization"] = tokens;
  print('开始网络请求');
  Response response = await dio.post(url + interface, queryParameters: {
    'type': type,
    'size': size,
  });
  print(response);
  if (response.statusCode == 200) {
    print("调用对象类");
    commentDto = CommentDto.fromJson(response.data["data"]);
  }
  return commentDto;

}


///推荐动态
///api:  /appuser/web/feed/pullHotList
Future getrecommendedNews(int size,String interface) async {
  CommentDto commentDto;
  Dio dio = Dio();
  dio.options.headers["aimy-drivers"] =
  '{\"os\":0,\"clientVersion\":\"1.0.0\",\"channel\":0}';
  dio.options.headers["Authorization"] = tokens;
  print('开始网络请求');
  Response response = await dio.post(url + interface, queryParameters: {
    'size': size,
  });
  print(response);
  if (response.statusCode == 200) {
    print("调用对象类");
    commentDto = CommentDto.fromJson(response.data["data"]);
  }
  return commentDto;
}



///登录基本信息
///api:/ucenter/web/user/perfectUserInfo
Future perfectUserInfo(String nickName,String avatarUri,String interface) async {
  Dio dio = Dio();
  dio.options.headers["aimy-drivers"] =
  '{\"os\":0,\"clientVersion\":\"1.0.0\",\"channel\":0}';
  dio.options.headers["Authorization"] = tokens;
  print('开始网络请求');
  Response response = await dio.post(url + interface, queryParameters: {
    'nickName': nickName,
    'avatarUri': avatarUri,
  });
  print(response);
  if (response.statusCode == 200) {
    print("调用对象类");
    return true;
  }
  return false;

}

///健身信息
///api:/appuser/web/user/saveBasicFitnessInfo
Future saveBasicFitnessInfo(SaveBasicFitnessInfo saveBasicFitnessInfo,String interface) async {
  print("使用的token------------------------------");
  print("$tokens");

  Dio dio = Dio();
  dio.options.headers["aimy-drivers"] =
  '{\"os\":0,\"clientVersion\":\"1.0.0\",\"channel\":0}';
  dio.options.headers["Authorization"] = tokens;
  print('开始网络请求');
  Response response = await dio.post(url + interface, queryParameters: {
    'height': saveBasicFitnessInfo.heights,
    'weight': saveBasicFitnessInfo.weight,
    'bodyType': saveBasicFitnessInfo.bodyType,
    'target': saveBasicFitnessInfo.target,
    'level': saveBasicFitnessInfo.level,
    'keyParts':saveBasicFitnessInfo.keyParts,
    'timesOfWeek': saveBasicFitnessInfo.timesOfWeek,
  });
  print(response);
  if (response.statusCode == 200) {
    print("调用对象类");
    return true;
  }
  return false;
}

///获取关注的用户信息
///api:/appuser/web/user/follow/QueryFollowingList
Future QueryFollowingList({int uid,int size,int lastTime,String interface}) async {
  print("------------------调用关注用户列表接口-----------------------");

  Attention attention=Attention();

  Dio dio = Dio();
  dio.options.headers["aimy-drivers"] =
  '{\"os\":0,\"clientVersion\":\"1.0.0\",\"channel\":0}';
  dio.options.headers["Authorization"] = tokens;
  print('开始网络请求');
  Response response = await dio.post(url + interface, queryParameters: {
    'uid': uid,
    'size': size,
    'lastTime':lastTime,
  });
  print(response);
  if (response.statusCode == 200) {
    print("调用对象类");
    attention = Attention.fromJson(response.data["data"]);
  }
  return attention;

}


///获取用户关注相关（粉丝数、关注数、动态数、获赞数）
///api:/appuser/web/user/getFollowCount
Future getFollowCount({int uid,String interface}) async {
  print("------------------调用关注用户列表接口-----------------------");
  GetFollowCounts getFollowCounts=GetFollowCounts();

  Dio dio = Dio();
  dio.options.headers["aimy-drivers"] =
  '{\"os\":0,\"clientVersion\":\"1.0.0\",\"channel\":0}';
  dio.options.headers["Authorization"] = tokens;
  print('开始网络请求');
  Response response = await dio.post(url + interface, queryParameters: {
    'uid': uid,
  });
  print(response);
  if (response.statusCode == 200) {
    print("调用对象类");
    getFollowCounts = GetFollowCounts.fromJson(response.data["data"]);
  }
  return getFollowCounts;
}


///用户关注
///api: /appuser/web/user/follow/addFollow
Future addFollow({int targetId,int type,String interface}) async {
  print("------------------调用关注用户接口-----------------------");
  Dio dio = Dio();
  dio.options.headers["aimy-drivers"] =
  '{\"os\":0,\"clientVersion\":\"1.0.0\",\"channel\":0}';
  dio.options.headers["Authorization"] = tokens;
  Response response = await dio.post(url + interface, queryParameters: {
    'targetId': targetId,
    'type':type,//0-普通场景1-直播中关注教练
  });
  if (response.data['code'] == 200) {
   return true;
  }
  return false;
}

///获取用户附加信息（健身时间、体重、相册数量）
///api:/appuser/web/user/getExtraInfo
Future getExtraInfo({int uid,String interface}) async {
  print("------------------调用关注用户列表接口-----------------------");
  UserWBodyInformation userWBodyInformation=UserWBodyInformation();

  Dio dio = Dio();
  dio.options.headers["aimy-drivers"] =
  '{\"os\":0,\"clientVersion\":\"1.0.0\",\"channel\":0}';
  dio.options.headers["Authorization"] = tokens;
  print('开始网络请求');
  Response response = await dio.post(url + interface);
  print(response);
  if (response.statusCode == 200) {
    print("调用对象类");
    userWBodyInformation = UserWBodyInformation.fromJson(response.data["data"]);
  }
  return userWBodyInformation;

}

///获取用户基础信息
///api:/ucenter/web/user/getUserBaseInfo
Future getUserBaseInfo({int uid,String interface}) async {
  print("------------------调用关注用户列表接口-----------------------");
  UserBaseInfoDto userBaseInfoDto=UserBaseInfoDto();

  Dio dio = Dio();
  dio.options.headers["aimy-drivers"] =
  '{\"os\":0,\"clientVersion\":\"1.0.0\",\"channel\":0}';
  dio.options.headers["Authorization"] = tokens;
  print('开始网络请求');
  Response response = await dio.post(url + interface, queryParameters: {
    'uid': uid,
  });
  print(response);
  if (response.statusCode == 200) {
    print("调用对象类");
    userBaseInfoDto = UserBaseInfoDto.fromJson(response.data["data"]);
  }
  return userBaseInfoDto;

}


///获取用户黑名单
///api：/appuser/web/black/queryList
Future getUserBlacklist({String interface}) async {
  print("------------------调用关注用户列表接口-----------------------");
  List<UserBlacklist> userBlacklistList=[];

  Dio dio = Dio();
  dio.options.headers["aimy-drivers"] =
  '{\"os\":0,\"clientVersion\":\"1.0.0\",\"channel\":0}';
  dio.options.headers["Authorization"] = tokens;
  print('开始网络请求');
  Response response = await dio.post(url + interface);
  print(response);
  if (response.data["code"] == 200) {
    print("调用对象类");
    //循环添加黑名单
    (response.data["data"]["list"] as List).forEach((element) {
      userBlacklistList.add(UserBlacklist.fromJson(element));
    });
  }
  return userBlacklistList;

}


///用户搜索
///api:/appuser/web/user/searchFollowUser
Future searchFollowUser({int uid,String interface,String key,int size,int lastTime}) async {
  print("------------------调用关注用户列表接口-----------------------");
  Attention attention=Attention();

  Dio dio = Dio();
  dio.options.headers["aimy-drivers"] =
  '{\"os\":0,\"clientVersion\":\"1.0.0\",\"channel\":0}';
  dio.options.headers["Authorization"] = tokens;
  print('开始网络请求');
  Response response = await dio.post(url + interface, queryParameters: {
    'key':key,
    'size':size,
    'uids': uid,
    'lastTime':lastTime
  });
  print(response);
  if (response.data["code"] == 200) {
    print("调用对象类");
    attention = Attention.fromJson(response.data["data"]);
  }
  return attention;
}


///获取用户记录的体重信息
///api：/appuser/web/user/getWeightRecords
Future getWeightRecords({String interface,int page,int size}) async {
  print("------------------调用用户记录--体重信息接口-----------------------");
  GetMyWigets getMyWigets=GetMyWigets();

  Dio dio = Dio();
  dio.options.headers["aimy-drivers"] =
  '{\"os\":0,\"clientVersion\":\"1.0.0\",\"channel\":0}';
  dio.options.headers["Authorization"] = tokens;
  print('开始网络请求');
  Response response = await dio.post(url + interface, queryParameters: {
    'page':page,
    'size':size,
  });
  print(response);
  if (response.data["code"] == 200) {
    print("调用对象类");
    getMyWigets = GetMyWigets.fromJson(response.data["data"]);
  }
  return getMyWigets;
}


///保存目标体重
///api: /appuser/web/user/saveTargetWeight
Future saveTargetWeights({String interface,double targetWeight}) async {
  print("------------------调用用户记录--体重信息接口-----------------------");
  Dio dio = Dio();
  dio.options.headers["aimy-drivers"] =
  '{\"os\":0,\"clientVersion\":\"1.0.0\",\"channel\":0}';
  dio.options.headers["Authorization"] = tokens;
  print('保存目标体重');
  Response response = await dio.post(url + interface, queryParameters: {
    'targetWeight':targetWeight,
  });
  print(response);
  if (response.data["code"] == 200) {
    return true;
  }
  return false;
}

///保存体重
///api: /appuser/web/user/saveWeight
Future saveWeight({String interface,double weight}) async {
  print("------------------调用用户记录--保存目标体重-----------------------");
  Dio dio = Dio();
  dio.options.headers["aimy-drivers"] =
  '{\"os\":0,\"clientVersion\":\"1.0.0\",\"channel\":0}';
  dio.options.headers["Authorization"] = tokens;
  Response response = await dio.post(url + interface, queryParameters: {
    'weight':weight,
  });
  print(response);
  if (response.data["code"] == 200) {
    return true;
  }
  return false;
}

///删除记录体重
///api：/appuser/web/user/delWeight
Future delWeight({String interface,int id}) async {
  print("------------------调用用户记录--保存目标体重-----------------------");
  Dio dio = Dio();
  dio.options.headers["aimy-drivers"] =
  '{\"os\":0,\"clientVersion\":\"1.0.0\",\"channel\":0}';
  dio.options.headers["Authorization"] = tokens;
  Response response = await dio.post(url + interface, queryParameters: {
    'id':id,
  });
  print(response);
  if (response.data["code"] == 200) {
    return true;
  }
  return false;
}